package com.achao.service.impl;

import com.achao.dao.SiteDao;
import com.achao.dao.TrainDao;
import com.achao.javabean.vo.TrainSite;
import com.achao.javabean.vo.TrainVo;
import com.achao.service.TrainService;
import com.achao.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: achao
 * @DateTime: 2022/7/26 20:49
 * @Description: TODO
 */
public class TrainServiceImpl implements TrainService {


    @Override
    public List<TrainVo> showInf(String start, String end) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        SiteDao siteDao = sqlSession.getMapper(SiteDao.class);
        TrainDao trainDao = sqlSession.getMapper(TrainDao.class);

        int startId = siteDao.getIdBySite(start);
        int endId = siteDao.getIdBySite(end);
        List<TrainSite> startList = siteDao.getTrainSiteBySiteId(startId);
        List<TrainSite> endList = siteDao.getTrainSiteBySiteId(endId);
        for (TrainSite trainSite : startList) {
            System.out.println(trainSite);
        }
        System.out.println("==================================");
        for (TrainSite trainSite : endList) {
            System.out.println(trainSite);
        }
//        ===================================
        List<TrainVo> list = new ArrayList<>();
        for (TrainSite trainSite1 : startList) {
            for (TrainSite trainSite2 : endList) {
                if (trainSite1.getTrainId() == trainSite2.getTrainId() && trainSite1.getSiteNum() < trainSite2.getSiteNum()) {

                    String trainName = trainDao.getTrainNameById(trainSite1.getTrainId());
                    String startName = siteDao.getSiteNameById(trainSite1.getSiteId());
                    String endName = siteDao.getSiteNameById(trainSite2.getSiteId());
                    TrainVo trainVo3 = new TrainVo(trainName, startName, endName, trainSite1.getStartTime(), trainSite2.getEndTime());
                    list.add(trainVo3);
                    endList.remove(trainSite2);
                    break;
                }
            }
        }

        for (TrainVo trainVo3 : list) {
            System.out.println(trainVo3);
        }


        return list;
    }
}
